
import BaseStorage from "./BaseStorage";
import { isNull } from "../tools/Define";

export default class LocalList extends BaseStorage {


    protected value: any[];


    get(index: number) {
        if (index >= 0 && index <= this.value.length - 1) {
            return this.value[index];
        } else {
            this.set(index, this.initValue)
            return this.initValue;
        }
    }

    protected loadValue() {
        let localValue = this.getStorage();
        if (!isNull(localValue)) {
            this.setValue(localValue)
            return true;
        } else {
            this.value = []
            return false
        }

    }


    size() {
        return this.value.length;
    }

    set(index: number, value: any, save: boolean = true) {
        if (this.value[index] != value) {
            this.value[index] = value;
            if (save) {
                this.saveValue();
            }
        }

    }

    remove(index) {
        if (!isNull(this.value[index])) {
            delete this.value[index]
            this.saveValue();
        }
    }
}
